package util.xmlmanager.elements;

import java.util.LinkedList;


import util.math.TokenOperator;
import util.xmlmanager.essential.Operator;

public class Attribute {
	//Constants:
	private static final byte NUMBER_OF_ARGUMENTS = 2;
	
	//Attributes:
	private String name;
	private String value;

	//Constructors:
	public Attribute(String name,String value){
		this.name = name;
		this.value = value;
	}
	//Methods:
	public String getName() {
		final String regEx = "(.+[|])|([)])";
		String[]split = this.name.split(regEx);
		String ans = (split.length==1)?split[0]:((split.length==2)?split[1]:null);
		return ans;
	}
	
	public String getValue() {
		return value;
	}
	
	public boolean equalsTo(Attribute attribute){
		return 	(attribute!=null)&&(((this.name==null&&attribute.getName()==null)||((this.name!=null&&attribute.getName()!=null)&&(attribute.getName().matches(this.name))))&&
				((this.value==null&&attribute.getValue()==null)||((this.value!=null&&attribute.getValue()!=null)&&(this.value.equals(attribute.getValue())))));
	}
	
	public boolean notEqualsTo(Attribute attribute){
		return 	(attribute!=null)&&(((this.name==null&&attribute.getName()==null)||((this.name!=null&&attribute.getName()!=null)&&(attribute.getName().matches(this.name))))&&
					(
						(this.value==null&&attribute.getValue()!=null)||(this.value!=null&&attribute.getValue()==null)||
						((this.value!=null&&attribute.getValue()!=null)&&(!this.value.equals(attribute.getValue())))
					)
				);
	}
	
	public boolean greaterThan(Attribute attribute){
		try{
			if((attribute!=null) && (this.value!=null&&attribute.getValue()!=null)){
				int value1,value2;
				value1 = Integer.parseInt(this.value);
				value2 = Integer.parseInt(attribute.getValue());
				return 	(((this.name==null&&attribute.getName()==null)||((this.name!=null&&attribute.getName()!=null)&&(attribute.getName().matches(this.name))))&&
						(value1 > value2));
			}
		}catch(NumberFormatException ignored){
			//FIXME How could be this? Is it need throws the exception?
		}
		return false;
	}
	public boolean greaterOrEqualTo(Attribute attribute){
		try{
			if((attribute!=null) && (this.value!=null&&attribute.getValue()!=null)){
				int value1,value2;
				value1 = Integer.parseInt(this.value);
				value2 = Integer.parseInt(attribute.getValue());
				return 	(((this.name==null&&attribute.getName()==null)||((this.name!=null&&attribute.getName()!=null)&&(attribute.getName().matches(this.name))))&&
						(value1 >= value2));
			}
		}catch(NumberFormatException ignored){
			//FIXME How could be this? Is it need throws the exception?
		}
		return false;
	}
	public boolean lessThan(Attribute attribute){
		try{
			if((attribute!=null) && (this.value!=null&&attribute.getValue()!=null)){
				int value1,value2;
				value1 = Integer.parseInt(this.value);
				value2 = Integer.parseInt(attribute.getValue());
				return 	(((this.name==null&&attribute.getName()==null)||((this.name!=null&&attribute.getName()!=null)&&(attribute.getName().matches(this.name))))&&
						(value1 < value2));
			}
		}catch(NumberFormatException ignored){
			//FIXME How could be this? Is it need throws the exception?
		}
		return false;
	}
	public boolean lessOrEqualTo(Attribute attribute){
		try{
			if((attribute!=null) && (this.value!=null&&attribute.getValue()!=null)){
				int value1,value2;
				value1 = Integer.parseInt(this.value);
				value2 = Integer.parseInt(attribute.getValue());
				return 	(((this.name==null&&attribute.getName()==null)||((this.name!=null&&attribute.getName()!=null)&&(attribute.getName().matches(this.name))))&&
						(value1 <= value2));
			}
		}catch(NumberFormatException ignored){
			//FIXME How could be this? Is it need throws the exception?
		}
		return false;
	}
	
	public String toString(){
		//return this.getName()+"=\""+this.value+"\"";
		return this.name+"=\""+this.value+"\"";
	}
	
	public static Attribute getAttribute(String atom){
		String regex = Operator.SPACE;
		for (int i=0;i<Operator.SYMBOL.length;i++) {
			regex += Operator.SYMBOL[i] + (((i+1)<Operator.SYMBOL.length)?"|":"");
		}
		regex += Operator.SPACE;
		LinkedList<String> tmp = TokenOperator.getClearList(atom.split(regex));
		if(tmp.size() == NUMBER_OF_ARGUMENTS){
			return new Attribute(tmp.getFirst(),tmp.getLast());
		}else{
			return null;
		}
	}
	public void setName(String name) {
		this.name = name;
	}
	//Main:
	public static void main(String[] args) {
		Attribute tmp1 = new Attribute("(D.name|name)","2fd");
		Attribute tmp2 = new Attribute("name","2fd");
		System.out.println(tmp2.toString()+" == "+tmp1.toString()+": "+tmp2.equalsTo(tmp1));
	}
}
